package com.jeesite.modules.sd.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.common.util.HttpUtil;
import com.jeesite.modules.sd.entity.FileCabinetRecord;
import com.jeesite.modules.sd.service.FileCabinetRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 模拟第三方：server.port=8080
 */
@Slf4j
@Controller
@RequestMapping("/sd/simulateThirdParty")
public class SimulateThirdPartyController extends BaseController {

    @Autowired
    private FileCabinetRecordService fileCabinetRecordService;

    /**
     * 跳转发送人脸结果页面
     */
    @RequestMapping(value = "sendFaceResult")
    public String sendFaceResult(FileCabinetRecord fileCabinetRecord, Model model) {

        model.addAttribute("fileCabinetRecord", fileCabinetRecord);
        return "modules/sd/sendFaceResult";
    }

    /**
     * 跳转发送开/关门结果页面
     */
    @RequestMapping(value = "sendCabinetCommand")
    public String sendCabinetCommand(FileCabinetRecord fileCabinetRecord, Model model) {

        model.addAttribute("fileCabinetRecord", fileCabinetRecord);
        return "modules/sd/sendCabinetCommand";
    }

    /**
     * 上传人脸信息
     *
     * @return
     */
    @RequestMapping(value = "uploadFile")
    @ResponseBody
    public String uploadFile(MultipartFile file) {
        JSONObject object = null;
        try {
            String url = "http://127.0.0.1:80/sd/interactingThirdParties/uploadFile";
            InputStream fileInputStream = file.getInputStream();
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", fileInputStream, ContentType.MULTIPART_FORM_DATA, "file");
            String s = HttpUtil.executeUrlOfBuilder(url, builder);
            object = JSONObject.parseObject(s);
            fileInputStream.close();
        } catch (Exception e) {
            log.error("上传人脸信息失败：{}", e);
        }
        if (null != object && "true".equals(object.get("status").toString())) {
            return renderResult(Global.TRUE, text(object.get("msg").toString()),
                    object.get("data").toString());
        } else {
            return renderResult(Global.FALSE, text("上传失败"));
        }
    }

    /**
     * 发送人脸结果
     *
     * @param fileCabinetRecord 人脸信息
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public String sendResultOfFaceRecognition(FileCabinetRecord fileCabinetRecord) {
        String url = "http://127.0.0.1:80/sd/interactingThirdParties/resultOfFaceRecognition";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("policeno", fileCabinetRecord.getPoliceno());
        jsonObject.put("userName", fileCabinetRecord.getUserName());
        jsonObject.put("userFace", fileCabinetRecord.getUserFace());
        jsonObject.put("responseStatus", fileCabinetRecord.getResponseStatus());
        String s = HttpUtil.executeUrlOfJson(url, jsonObject);
        JSONObject object = JSONObject.parseObject(s);
        return communalRenderResult(object);
    }

    /**
     * 发送开/关门结果
     *
     * @return
     */
    @RequestMapping(value = "send")
    @ResponseBody
    public String sendMailBoxResult(FileCabinetRecord fileCabinetRecord) {
        String responseStatus = fileCabinetRecord.getResponseStatus();
        fileCabinetRecord.setResponseStatus(null);
        JSONObject object = null;
        // 查询该流程记录
        List<FileCabinetRecord> fileCabinetRecordServiceList = fileCabinetRecordService
                .findList(fileCabinetRecord);
        FileCabinetRecord record = fileCabinetRecordServiceList.get(0);
        if (Objects.nonNull(record)) {

            String url = "http://127.0.0.1:80/sd/interactingThirdParties/mailBoxResult";
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("processCode", fileCabinetRecord.getProcessCode());
            jsonObject.put("responseStatus", responseStatus);
            jsonObject.put("responseType", fileCabinetRecord.getResponseType());
            String s = HttpUtil.executeUrlOfJson(url, jsonObject);
            object = JSONObject.parseObject(s);
        }
        return communalRenderResult(object);
    }

    /**
     * 接收开门命令
     *
     * @param fileCabinetRecord
     * @return
     */
    @PostMapping("/openMailBox")
    public JSONObject testOpenMailBox(@RequestBody FileCabinetRecord fileCabinetRecord) {
        if (Objects.nonNull(fileCabinetRecord)) {
            log.info(fileCabinetRecord.toString());
        }
        // 返回信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "true");
        jsonObject.put("msg", "接收成功");
        return jsonObject;
    }

    /**
     * 公用返回信息
     *
     * @param object
     * @return
     */
    public String communalRenderResult(JSONObject object) {
        if (null != object) {
            if ("true".equals(object.get("status").toString())) {
                return renderResult(Global.TRUE, text(object.get("msg").toString()));
            } else {
                return renderResult(Global.FALSE, text(object.get("msg").toString()));
            }
        } else {
            return renderResult(Global.FALSE, text("操作失败"));
        }
    }

    /**
     * 跳转扫描二维码页面
     */
    @RequestMapping(value = "sendMsg")
    public String sendMsg(FileCabinetRecord fileCabinetRecord, Model model) {
        model.addAttribute("fileCabinetRecord", fileCabinetRecord);
        return "modules/sd/scanQRCode";
    }
}
